'use strict';
app.controller('ClientUserCtrl', ['$scope', '$modal', '$rootScope', '$state', '$filter', 'toaster', 'commonService', 'userService', function ($scope, $modal, $rootScope, $state, $filter, toaster, commonService, userService) {
    $scope.page_code = "clientUser";
    $scope.matchString = "";

    function getTbDefinitionsFromServer() {
        return {
            "serverSide": true,
            "ajax": {
                url: "clientUser/plist", data: function (d) {
                    d.matchString = $scope.matchString;
                    for (var key in d) {
                        if (key.indexOf("columns") == 0) {
                            delete d[key];
                        }
                    }
                }
            },
            aoColumns: [
                {mData: 'id'},
                {mData: 'name'},
                {mData: 'sex'},
                {mData: 'cellphone'},
                {mData: 'address'},
                {mData: 'bof'},
                {mData: 'account'}
               ],
            aoColumnDefs: [{
                aTargets: [0], mRender: function (data, type, full) {
                    var editId = "clientUser" + data;
                    return '<label class="i-checks m-b-none"><input id = "' + editId + '" class="selectListCmPara" type="checkbox"><i></i></label>';
                }, bSortable: false
            },commonService.tableRowAllowNullNoSort(1),
                commonService.tableRowAllowNullNoSort(2),
                commonService.tableRowAllowNullNoSort(3),
                commonService.tableRowAllowNullNoSort(4),
                {
                    aTargets : [ 5 ],
                    mRender : function(data) {
                        if(data){
                            var date = $filter('date')(new Date(data),'yyyy-MM-dd');
                            return date;
                        }
                        return "";
                    },
                    bSortable:false
                },
                commonService.tableRowAllowNullNoSort(6)

            ],
            fnDrawCallback: function (oSettings, json) {
                $(".selectListCmPara").on('click', function () {
                    $(".selectListCmPara").not(this).attr("checked", false);
                });
            }
        };
    };
    $scope.tbOptions = getTbDefinitionsFromServer();
    $scope.add = function () {
        var param = {type: 'create'};
        commonService.openEditDialog(param, 'edit', 'ClientUserEditCtrl');
    };
    $scope.edit = function () {
        var info = commonService.getSelectData("clientUserTable", "clientUser", "id");
        if (info != null) {
            var param = {type: 'edit', info: info};
            commonService.openEditDialog(param, 'edit', 'ClientUserEditCtrl');
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.delete = function () {
        var info = commonService.getSelectData("clientUserTable", "clientUser", "id");
        if (info != null) {
            BootstrapDialog.confirm("确认要删除：" + info.name, function (result) {
                if (result) {
                    commonService.httpJson("clientUser/ditem", {id: info.id}).success(function (data, status) {
                        if (data && data['code'] == 0) {
                            toaster.pop('info', '提示', data['message']);
                            $scope.refresh();
                        } else {
                            commonService.toasterError(data['message']);
                        }
                    });
                }
            });
        } else {
            toaster.pop('info', '提示', '请选择需要修改的信息。');
        }
    };
    $scope.refresh = function () {
        var table = $('#clientUserTable').DataTable();
        table.ajax.reload();
    };
    $scope.search = function () {
        $scope.refresh();
    };
    $scope.$on('clientUser.update', function (event) {
        $scope.refresh();
    });
}]);
app.controller('ClientUserEditCtrl',
    ['$scope', '$rootScope', '$modalInstance',
        'param', 'toaster', 'commonService', 'userService','$http',
        function ($scope, $rootScope, $modalInstance, param,
                  toaster, commonService, userService,$http) {
    $scope.clientUser = {};
    $scope.ImgName = "images/no.png";

    $scope.uploadfile = function () {
        var form = new FormData();
        var file = document.getElementById("file").files[0];
        form.append('file', file);
        $http({
            method: 'POST',
            url: 'fileUpload/up',
            data: form,
            headers: {'Content-Type': undefined},
            transformRequest: angular.identity
        }).success(function (data) {
            if (data) {
                $scope.ImgName = getImgName(data['data']['realName']);
                $scope.clientUser.headimg = data['data']['id'];
                commonService.toasterSuccess(data['message']);
            }
        }).error(function (data) {
            console.log('operation fail');
            commonService.toasterError(data['message']);
        })

    };
    function getImgName(diskFileName) {
        if (diskFileName) {
            return "upImg/img_file/" + diskFileName;
        } else {
            return "images/no.png";
        }
    }
    if (param.type == "edit") {
        $scope.ImgName = getImgName(param.info.fileName);
        $scope.clientUser = param.info;

    }
    function checkSave() {
        var result = true;
        if (!$scope.clientUser.name) {
            commonService.toasterError("名称不能为空");
            result = false;
        }
        return result;
    }

    $scope.submit = function () {
        if (!checkSave()) {
            return;
        }
        var salt = "1a2b3c4d5e6f7g8h9j0k";
        $scope.clientUser.password = hex_md5(salt.charAt(0)+salt.charAt(2)+$scope.clientUser.password+salt.charAt(5)+salt.charAt(9));
        if (param.type == "edit") {
            $scope.clientUser.updateUser = userService.getId();
        } else {
            $scope.clientUser.createUser = userService.getId();
        }
        commonService.httpJson("clientUser/save", $scope.clientUser).success(function (data) {
            if (data && data['code'] == 0) {
                commonService.editData("clientUser.update", param.type);
                toaster.pop("info", "信息", data['message']);
                $modalInstance.close();
            } else {
                commonService.toasterError(data['message']);
            }
        });
    }
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}]);